package ui;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import pd.*;

public class PrefPane extends JFrame {

	private static final long serialVersionUID = 1L;
	private SwissSMSClient swissSMSClient;
	private JComboBox services;
	private JTextField username = new JTextField();
	private JPasswordField password = new JPasswordField();
	private JButton cancel = new JButton("Cancel");
	private JButton save = new JButton("Save");
	private JCheckBox useAppleAB = new JCheckBox("Use Apple Addressbook");
	

	public PrefPane(SwissSMSClient swissSMSClient){
		super("Preferences");
		this.swissSMSClient = swissSMSClient;
		getContentPane().setLayout(new BorderLayout());
		initUI();
		initActions();
		setResizable(false);
		pack();
		setLocationRelativeTo(null);
	}
	
	private void initActions(){
		services.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String servicename = (String) services.getSelectedItem();
				username.setText(swissSMSClient.getSetting(servicename).get(0));
				password.setText(swissSMSClient.getSetting(servicename).get(1));
			}
		});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String servicename = swissSMSClient.getSetting("service").get(0);
				username.setText(swissSMSClient.getSetting(servicename).get(0));
				password.setText(swissSMSClient.getSetting(servicename).get(1));
				setVisible(false);
			}
		});
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String servicename = (String) services.getSelectedItem();
				swissSMSClient.getSetting("service").set(0, servicename);
				swissSMSClient.getSetting(servicename).set(0, username.getText());
				swissSMSClient.getSetting(servicename).set(1, new String(password.getPassword()));
				if(System.getProperty("os.name").contains("Mac OS X")){
					if(useAppleAB.isSelected()){
						swissSMSClient.getSetting("useappleaddrbook").set(0, "1");
					} else {
						swissSMSClient.getSetting("useappleaddrbook").set(0, "0");
					}
				}
				swissSMSClient.getStorage().saveSettings();
				setVisible(false);
			}
		});
	}
	
	private void initUI(){
		String servicename = swissSMSClient.getSetting("service").get(0);
		
		Box inputbox = Box.createVerticalBox();
		inputbox.add(Box.createVerticalStrut(20));
		
		Box servicesbox = Box.createHorizontalBox();
		servicesbox.add(Box.createHorizontalStrut(20));
		services = new JComboBox(swissSMSClient.getStorage().getServices());
		services.setSelectedItem(servicename);
		setSize(services, 170, 30);
		servicesbox.add(services);
		servicesbox.add(Box.createHorizontalGlue());
		inputbox.add(servicesbox);
		inputbox.add(Box.createVerticalStrut(10));
		
		Box userbox = Box.createHorizontalBox();
		userbox.add(Box.createHorizontalStrut(20));
		userbox.add(new JLabel("Username: "));
		username.setText(swissSMSClient.getSetting(servicename).get(0));
		setSize(username, 100, 30);
		userbox.add(username);
		userbox.add(Box.createHorizontalGlue());
		inputbox.add(userbox);
		inputbox.add(Box.createVerticalStrut(10));
		
		Box passwdbox = Box.createHorizontalBox();
		passwdbox.add(Box.createHorizontalStrut(20));
		passwdbox.add(new JLabel("Password: "));
		password.setText(swissSMSClient.getSetting(servicename).get(1));
		setSize(password, 100, 30);
		passwdbox.add(password);
		passwdbox.add(Box.createHorizontalGlue());
		inputbox.add(passwdbox);
		inputbox.add(Box.createVerticalStrut(10));
		
		if(System.getProperty("os.name").contains("Mac OS X")){
			Box appleusebox = Box.createHorizontalBox();
			appleusebox.add(Box.createHorizontalStrut(20));
			useAppleAB.setSelected(swissSMSClient.getSetting("useappleaddrbook").get(0).matches("1"));
			appleusebox.add(useAppleAB);
			appleusebox.add(Box.createHorizontalGlue());
			inputbox.add(appleusebox);
			inputbox.add(Box.createVerticalStrut(10));
		}
		
		Box buttonsbox = Box.createHorizontalBox();
		buttonsbox.add(Box.createHorizontalGlue());
		buttonsbox.add(cancel);
		buttonsbox.add(save);
		buttonsbox.add(Box.createHorizontalStrut(20));
		inputbox.add(buttonsbox);
		inputbox.add(Box.createVerticalStrut(10));
		
		getContentPane().add(inputbox, BorderLayout.NORTH);
	}
	
	private static void setSize(Component c, int dx, int dy){
		c.setPreferredSize(new Dimension(dx,dy));
		c.setMinimumSize(new Dimension(dx,dy));
		c.setMaximumSize(new Dimension(dx,dy));
	}

}
